package com.xsd.awen.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class JdbcUtils {
    private static Logger logger = LoggerFactory.getLogger(JdbcUtils.class);


    public static Connection connect(String url,String username,String password) {
        Connection connection = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection(url, username, password);
        } catch (Exception e) {
            logger.warn(e.getMessage(), e);
            throw new RuntimeException(e);
        }
        return connection;
    }

    public static Connection connect() {
        String url = "jdbc:mysql://127.00.1:3306/eims?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8";
        String username = "root";
        String password = "root";
        return connect(url,username,password);
    }

    public static void disconnect(Connection connection) {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (Exception e) {
            logger.warn(e.getMessage(), e);
            throw new RuntimeException(e);
        }
    }

    public static void closeRs(ResultSet resultSet){
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (Exception e) {
                logger.warn(e.getMessage(), e);
            }
        }
    }

    public static void closePs(PreparedStatement ps){
        if (ps != null) {
            try {
                ps.close();
            } catch (Exception e) {
                logger.warn(e.getMessage(), e);
            }
        }
    }
}